package com.colin.app.activity.senior.pullrefresh;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;

import com.colin.app.R;
import com.colin.app.activity.senior.SeniorContract;
import com.colin.app.activity.senior.SeniorPresenterImpl;
import com.colin.app.adapter.ListAdapter;
import com.colin.app.adapter.RecyclerItemBeanAdapter;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.app.bean.ItemBean;
import com.colin.library.help.ImageLoader;
import com.colin.library.view.custom.PullRefreshLayout;

import java.util.List;

public class PullRefreshCustomActivity extends AppActivity implements SeniorContract.SeniorView {
    private SeniorContract.SeniorPresenter mSeniorPresenter = null;
    private PullRefreshLayout pull_refresh_list = null;
    private PullRefreshLayout pull_refresh_recycler = null;
    private PullRefreshLayout pull_refresh_scroll = null;
    private RecyclerView recycler_pull_refresh_child = null;
    private ScrollView scroll_pull_refresh = null;
    private ListView list_pull_refresh_child = null;
    private ImageView image_pull_refresh_show = null;
    private ListAdapter adapter = null;
    private RecyclerItemBeanAdapter recyclerItemBeanAdapter = null;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            addData(msg.what == 1);
        }
    };

    private void addData(boolean isRefresh) {
        List<ItemBean> itemBeanList = mSeniorPresenter.listGetItemData(isRefresh);
        if (mSeniorPresenter.getPullRefreshViewChild() == 0 && isRefresh) {
            adapter.refreshItems(itemBeanList);
            pull_refresh_list.refreshFinished();
        } else if (mSeniorPresenter.getPullRefreshViewChild() == 0 && !isRefresh) {
            adapter.addItems(itemBeanList);
            pull_refresh_list.loadMoreFinished();
        } else if (mSeniorPresenter.getPullRefreshViewChild() == 1 && isRefresh) {
            recyclerItemBeanAdapter.refresh(itemBeanList);
            pull_refresh_recycler.refreshFinished();
        } else if (mSeniorPresenter.getPullRefreshViewChild() == 1 && !isRefresh) {
            recyclerItemBeanAdapter.addItems(itemBeanList);
            pull_refresh_recycler.loadMoreFinished();
        } else if (mSeniorPresenter.getPullRefreshViewChild() == 2 && isRefresh) {
            ImageLoader.getInstance().loadImageByGlide(image_pull_refresh_show, itemBeanList.get(0).getImageLarge());
            pull_refresh_scroll.refreshFinished();
        } else if (mSeniorPresenter.getPullRefreshViewChild() == 2 && !isRefresh) {
            ImageLoader.getInstance().loadImageByGlide(image_pull_refresh_show, itemBeanList.get(1).getImageLarge());
            pull_refresh_scroll.loadMoreFinished();
        }
    }

    @Override
    protected void onDestroy() {
        if (null != mSeniorPresenter) {
            mSeniorPresenter.destroy();
            mSeniorPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mSeniorPresenter = new SeniorPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_pull_refresh_custom;
    }

    @Override
    public void initView() {
        this.pull_refresh_list = (PullRefreshLayout) this.findViewById(R.id.pull_refresh_list);
        this.pull_refresh_recycler = (PullRefreshLayout) this.findViewById(R.id.pull_refresh_recycler);
        this.pull_refresh_scroll = (PullRefreshLayout) this.findViewById(R.id.pull_refresh_scroll);

        this.list_pull_refresh_child = (ListView) this.findViewById(R.id.list_pull_refresh_child);
        this.recycler_pull_refresh_child = (RecyclerView) this.findViewById(R.id.recycler_pull_refresh_child);
        this.scroll_pull_refresh = (ScrollView) this.findViewById(R.id.scroll_pull_refresh);
        this.image_pull_refresh_show = (ImageView) this.findViewById(R.id.image_pull_refresh_show);
        initPullRefreshView(0);

    }

    private void initPullRefreshView(int position) {
        mSeniorPresenter.setPullRefreshViewChild(position);

        pull_refresh_list.setVisibility(View.GONE);
        pull_refresh_recycler.setVisibility(View.GONE);
        pull_refresh_scroll.setVisibility(View.GONE);
        if (position == 0) {
            pull_refresh_list.setVisibility(View.VISIBLE);
            initListView();
        } else if (position == 1) {
            pull_refresh_recycler.setVisibility(View.VISIBLE);
            initRecyclerView();
        } else if (position == 2) {
            pull_refresh_scroll.setVisibility(View.VISIBLE);
            initImageView();
        }
    }


    private void initListView() {
        if (null == adapter) {
            adapter = new ListAdapter(this, mSeniorPresenter.listGetItemData(true));
        }
        list_pull_refresh_child.setAdapter(adapter);
    }

    private void initRecyclerView() {
        if (null == recyclerItemBeanAdapter) {
            recyclerItemBeanAdapter = new RecyclerItemBeanAdapter(this, mSeniorPresenter.listGetItemData(true), -1, null);
        }
        final int grid_columns = getResources().getInteger(R.integer.grid_columns);
        RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(grid_columns, OrientationHelper.VERTICAL);
        this.recycler_pull_refresh_child.setLayoutManager(layoutManager);
        this.recycler_pull_refresh_child.setAdapter(recyclerItemBeanAdapter);
    }

    private void initImageView() {
        ImageLoader.getInstance().loadImageByGlide(image_pull_refresh_show, mSeniorPresenter.listGetItemData(true).get(0).getImageLarge());
    }

    @Override
    public void initData() {
        mSeniorPresenter.setData();
    }

    @Override
    public void initListener() {
        this.pull_refresh_list.addPullRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void refreshFinished() {
                mHandler.sendEmptyMessageDelayed(1, 3 * 1000);
            }

            @Override
            public void loadMoreFinished() {
                mHandler.sendEmptyMessageDelayed(2, 3 * 1000);
            }
        });

        this.pull_refresh_recycler.addPullRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void refreshFinished() {
                mHandler.sendEmptyMessageDelayed(1, 3 * 1000);
            }

            @Override
            public void loadMoreFinished() {
                mHandler.sendEmptyMessageDelayed(2, 3 * 1000);
            }
        });
        this.pull_refresh_scroll.addPullRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void refreshFinished() {
                mHandler.sendEmptyMessageDelayed(1, 3 * 1000);
            }

            @Override
            public void loadMoreFinished() {
                mHandler.sendEmptyMessageDelayed(2, 3 * 1000);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_pull_refresh_child, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        int position = 0;
        switch (item.getItemId()) {
            case R.id.pull_refresh_child_list:
                position = 0;
                break;
            case R.id.pull_refresh_child_recycler:
                position = 1;
                break;
            case R.id.pull_refresh_child_scroll:
                position = 2;
                break;
        }
        initPullRefreshView(position);
        item.setChecked(true);
        return super.onOptionsItemSelected(item);
    }
}
